﻿using ElectronicShoppingMall.Service.Data;
using ElectronicShoppingMall.Service.Models.Enterprise;
using ElectronicShoppingMall.Service.Models.Shared;
using ElectronicShoppingMall.Service.Services.Enterprise;
using ElectronicShoppingMall.Web.Back.Filter;
using ElectronicShoppingMall.Web.Back.DTOS;
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Http;

namespace ElectronicShoppingMall.Web.Back.Controllers
{
    public class GoodsController : ApiController
    {
        private GoodsService _serviceGoods = new GoodsService();
        //添加商品
        [HttpPatch]
        [Authorize]
        [EmployeePermission(PermissionCode = Permissions.InNewStocks)]
        public IHttpActionResult AddGoods(GoodsInfoDTO goods)
        {
            try
            {
                GoodsInfoModel goodsInfo = new GoodsInfoModel()
                {
                    GoodsName = goods.GoodsName,
                    Price = goods.Price,
                    Stock = goods.Stock,
                    Details = goods.Details,
                    Category = goods.Category,
                    Describe = goods.Describe
                };
                return Ok(_serviceGoods.AddGoods(goodsInfo));
            }
            catch
            {
                return null;
            }

        }
        //添加商品图片
        public IHttpActionResult AddGoodsImages()
        {
            try
            {
                HttpFileCollection files = HttpContext.Current.Request.Files;
                var id = HttpContext.Current.Request.Form.GetValues("goodsId");
                List<string> images = new List<string>();
                foreach (string key in files.AllKeys)
                {
                    HttpPostedFile file = files[key];
                    if (string.IsNullOrEmpty(file.FileName) == false)
                    {
                        var myGuid = Guid.NewGuid().ToString();
                        file.SaveAs("D:/stage-file/goodsImg/" + myGuid + file.FileName);
                        images.Add(myGuid + file.FileName);
                    }

                }
                _serviceGoods.AddGoodsImages(Convert.ToInt32(id[0]), images);
                return Ok("success");
            }
            catch
            {
                return null;
            }

        }
        //获取所有商品信息
        [HttpGet]
        [Authorize]
        public List<GoodsInfoModel> GetAllGoodsInfo()
        {
            try
            {
                var result = _serviceGoods.GetAllGoodsInfo();
                return result;
            }
            catch
            {
                return null;
            }
        }

        // 商品上下架
        [HttpPut]
        [Authorize]
        [EmployeePermission(PermissionCode = Permissions.CommodityUpperAndLowerFrame)]
        public List<GoodsInfoModel> ChangeGoodsState([FromBody]string goodsId)
        {
            try
            {
                _serviceGoods.ChangeGoodsState(Convert.ToInt32(goodsId));
                return GetAllGoodsInfo();
            }
            catch
            {
                return null;
            }
        }
        //库管退回商品
        [HttpPost]
        [Authorize]
        [EmployeePermission(PermissionCode = Permissions.RenturnGoods)]
        public List<GoodsInfoModel> ReturnGoods(dynamic objects)
        {
            try
            {
                _serviceGoods.ReturnGoods(Convert.ToInt32(objects.goodsId), Convert.ToInt32(objects.stock));
                return GetAllGoodsInfo();
            }
            catch
            {
                return null;
            }

        }
        //修改商品信息
        [HttpPatch]
        [Authorize]
        [EmployeePermission(PermissionCode = Permissions.ModificationOfCommodityInformation)]
        public List<GoodsInfoModel> ModificationOfCommodityInformation(GoodsInfoDTO goods)
        {
            try
            {
                GoodsInfoModel goodsInfo = new GoodsInfoModel()
                {
                    GoodsId = Convert.ToInt32(goods.GoodsId),
                    GoodsName = goods.GoodsName,
                    Price = goods.Price,
                    Stock = goods.Stock,
                    Details = goods.Details,
                    Category = goods.Category,
                    Describe = goods.Describe
                };
                _serviceGoods.ModificationOfCommodityInformation(goodsInfo);
                return GetAllGoodsInfo();
            }
            catch
            {
                return null;
            }
        }
        //修改新品推荐
        [System.Web.Mvc.HttpPost]
        [Authorize]
        [EmployeePermission(PermissionCode = Permissions.NewProductRecommendationManagement)]
        [OperationLog(OperatDetail =Operations.ChangeNewProduct)]
        public IHttpActionResult ChangeNewProductRecommendation()
        {
            try
            {
                HttpFileCollection files = HttpContext.Current.Request.Files;
                var goodsId = HttpContext.Current.Request.Form.GetValues("goodsId");
                var id = HttpContext.Current.Request.Form.GetValues("newProductId");
                List<string> images = new List<string>();
                foreach (string key in files.AllKeys)
                {
                    HttpPostedFile file = files[key];
                    if (string.IsNullOrEmpty(file.FileName) == false)
                    {
                        var myGuid = Guid.NewGuid().ToString();
                        file.SaveAs("D:/stage-file/newProduct/" + myGuid + file.FileName);
                        images.Add(myGuid + file.FileName);
                    }

                }
                _serviceGoods.ChangeNewProductRecommendation(Convert.ToInt32(id[0]),Convert.ToInt32(goodsId[0]), images);
                return Ok("ok");
            }
            catch
            {
                return null;
            }

        }
        [System.Web.Mvc.HttpGet]
        [Authorize]
        [EmployeePermission(PermissionCode = Permissions.NewProductRecommendationManagement)]
        public List<NewProductModel> GetNewProduct()
        {
            try
            {
                return _serviceGoods.GetNewProduct();
            }
            catch
            {
                return null;
            }
        }

    }
}
